﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Collections.Specialized;
using System.Collections;
using System.Text;
using System.Windows.Forms;
namespace  DIWAGIS.Tools
{
    class ConfigUtil
    {
      public static string GetHelpFile()
      {
        string helpFile = "";
        string AppPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        
        if (AppPath.IndexOf("file:\\") >= 0)
         AppPath = AppPath.Replace("file:\\", "");
        string fullAssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
        string assemblyName = fullAssemblyName.Split('.').GetValue(fullAssemblyName.Split('.').Length - 1).ToString();

        if (File.Exists(Path.Combine(System.IO.Directory.GetParent(AppPath).ToString(), "help\\" + assemblyName + ".hlp")))
          helpFile = Path.Combine(System.IO.Directory.GetParent(AppPath).ToString(), "help\\" + assemblyName + ".hlp");
        else if (File.Exists(Path.Combine(AppPath, "help\\" + assemblyName + ".hlp")))
          helpFile = Path.Combine(AppPath, "help\\" + assemblyName + ".hlp");
        return helpFile;
 
      }
      private string[] GetConfigFiles()
      {
        string AppPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
       
        if (AppPath.IndexOf("file:\\") >= 0)
          AppPath = AppPath.Replace("file:\\", "");
        
        
        string[] pConfigFiles = new string[1];
        string fullAssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
        string assemblyName = fullAssemblyName.Split('.').GetValue(fullAssemblyName.Split('.').Length - 1).ToString();
     
          if (File.Exists(Path.Combine(System.IO.Directory.GetParent(AppPath).ToString(), assemblyName + ".Config")))
          pConfigFiles[0] = Path.Combine(System.IO.Directory.GetParent(AppPath).ToString(), assemblyName + ".Config");
        else if (File.Exists(Path.Combine(AppPath, assemblyName + ".Config")))
          pConfigFiles[0] = Path.Combine(AppPath, assemblyName + ".Config");

        return pConfigFiles;
      }

      public double GetConfigValue(string keyname, double defaultValue)
      {

        try
        {
          string[] pConfigFiles = GetConfigFiles();
          string keyvalue = "";

          foreach (string configFile in pConfigFiles)
          {
            if (File.Exists(configFile))
            {
              XmlDocument oXml = new XmlDocument();
              oXml.Load(configFile);

              XmlNodeList oList = oXml.GetElementsByTagName("appSettings");
              if (oList == null) return defaultValue;

              foreach (XmlNode oNode in oList)
              {
                foreach (XmlNode oKey in oNode.ChildNodes)
                {
                  if ((oKey != null) && (oKey.Attributes != null))
                  {
                    if (oKey.Attributes["key"].Value.Equals(keyname))
                    {
                      if (oKey.Attributes["value"].Value.Trim().Length > 0)
                      {
                        keyvalue = oKey.Attributes["value"].Value;

                        //Try to convert to double
                        if (IsDouble(keyvalue))
                        {
                          return (Convert.ToDouble(keyvalue));
                        }
                      }
                    }
                  }
                }
              }
            }
          }

          return defaultValue;
        }
        catch (Exception ex)
        {
          System.Windows.Forms.MessageBox.Show(ex.Message);
          return defaultValue;
        }
      }
      public int GetConfigValue(string keyname, int defaultValue) 
      {

          try
          {
              string[] pConfigFiles = GetConfigFiles();
              string keyvalue = "";

              foreach (string configFile in pConfigFiles)
              {
                  if (File.Exists(configFile))
                  {
                      //NameValueCollection AppSettings = new NameValueCollection();
                      XmlDocument oXml = new XmlDocument();

                      oXml.Load(configFile);

                      XmlNodeList oList  = oXml.GetElementsByTagName("appSettings");
                      if (oList == null) return defaultValue;

                      //AppSettings = new NameValueCollection();
                      foreach (XmlNode oNode in oList)
                      {
                          foreach (XmlNode oKey in oNode.ChildNodes)
                          {
                              if ((oKey != null) && (oKey.Attributes != null) )
                              {
                                  if (oKey.Attributes["key"].Value.Equals(keyname)) 
                                  {
                                      if (oKey.Attributes["value"].Value.Trim().Length > 0) 
                                      {
                                          keyvalue = oKey.Attributes["value"].Value;
                                             
                                          //Try to convert to integer32
                                          if (IsInteger(keyvalue))
                                          {
                                              return (Convert.ToInt32(keyvalue));
                                          }      
                                      }
                                  }
                              }
                          }
                      }
                  }
              }

              return  defaultValue;
          }
          catch (Exception ex) 
          {
              System.Windows.Forms.MessageBox.Show(ex.Message);
              return defaultValue;
          }
      }
      public bool GetConfigValue(string keyname, bool defaultValue)
      {

          try
          {
              string[] pConfigFiles = GetConfigFiles();
              string keyvalue = "";

              foreach (string configFile in pConfigFiles)
              {
                  if (File.Exists(configFile))
                  {
                      //NameValueCollection AppSettings = new NameValueCollection();
                      XmlDocument oXml = new XmlDocument();

                      oXml.Load(configFile);

                      XmlNodeList oList = oXml.GetElementsByTagName("appSettings");
                      if (oList == null) return defaultValue;

                      //AppSettings = new NameValueCollection();
                      foreach (XmlNode oNode in oList)
                      {
                          foreach (XmlNode oKey in oNode.ChildNodes)
                          {
                              if ((oKey != null) && (oKey.Attributes != null))
                              {
                                  if (oKey.Attributes["key"].Value.Equals(keyname))
                                  {
                                      if (oKey.Attributes["value"].Value.Trim().Length > 0)
                                      {
                                          keyvalue = oKey.Attributes["value"].Value;

                                          //Try to convert to integer32
                                          if (IsBoolean(keyvalue))
                                          {
                                              return (Convert.ToBoolean(keyvalue));
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
              }

              return defaultValue;
          }
          catch (Exception ex)
          {
              System.Windows.Forms.MessageBox.Show(ex.Message);
              return defaultValue;
          }
      }
      public string GetConfigValue(string keyname, string defaultValue)
      {

          try
          {
              string[] pConfigFiles = GetConfigFiles();
              string keyvalue = "";

              foreach (string configFile in pConfigFiles)
              {
                  if (File.Exists(configFile))
                  {
                      //NameValueCollection AppSettings = new NameValueCollection();
                      XmlDocument oXml = new XmlDocument();
                    
                      oXml.Load(configFile);

                      XmlNodeList oList = oXml.GetElementsByTagName("appSettings");
                      if (oList == null) return defaultValue;

                      //AppSettings = new NameValueCollection();
                      foreach (XmlNode oNode in oList)
                      {
                          foreach (XmlNode oKey in oNode.ChildNodes)
                          {
                              if ((oKey != null) && (oKey.Attributes != null))
                              {
                                  if (oKey.Attributes["key"].Value.Equals(keyname))
                                  {
                                      if (oKey.Attributes["value"].Value.Trim().Length > 0)
                                      {
                                          keyvalue = oKey.Attributes["value"].Value;

                                          //Try to convert to integer32
                                          return keyvalue;
                                      }
                                  }
                              }
                          }
                      }
                  }
              }

              return defaultValue;
          }
          catch (Exception ex)
          {
              System.Windows.Forms.MessageBox.Show(ex.Message);
              return defaultValue;
          }
      }

      private Boolean KeyExists(XmlDocument xmlDoc, string strKey)
      {
          XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");

          // Attempt to locate the requested setting.
          foreach (XmlNode childNode in appSettingsNode)
          {
              if (childNode != null)
              {
                  if (childNode.NodeType == XmlNodeType.Element)
                  {
                      if (childNode.Attributes.Count > 0)
                      {
                          if (childNode.Attributes["key"] != null)
                          {
                              if (childNode.Attributes["key"].Value != null)
                              {
                                  if (childNode.Attributes["key"].Value == strKey)
                                  {
                                      return true;
                                  }
                              }
                          }
                      }
                  }
              }

          }
          return false;

      }

      private static bool IsDouble(string theValue)
  {
      try
      {
          Convert.ToDouble(theValue);
          return true;
      } 
      catch 
      {
          return false;
      }
  }  //IsDecimal
      private static bool IsInteger(string theValue)
  {
      try
      {
          Convert.ToInt32(theValue);
          return true;
      } 
      catch 
      {
          return false;
      }
  } //IsInteger
      private static bool IsBoolean(string theValue)
  {
      try
      {
          Convert.ToBoolean(theValue);
          return true;
      } 
      catch 
      {
          return false;
      }
  } //IsBoolean

    }
}

